﻿using System;
using System.Globalization;
using QAToolsAddin.GenericDialogs;
using QAToolsAddin.GenericDialogs.Fields;
using Excel = Microsoft.Office.Interop.Excel;

namespace QAToolsAddin.UI {
    class Date {
        private readonly Excel.Application _excelapp;
        private readonly Excel.Range _range;
        private CultureInfo _culture;
        private DateTime _minDateTime;

        public Date(Excel.Application application, CultureInfo culture) {
            try {
                _excelapp = application;
                _culture = culture;
                _range = application.ActiveWindow.RangeSelection;
                _minDateTime = new DateTime(1900, 1, 1);
            } catch (Exception ex) { new FrmException(_excelapp, ex).ShowDialog(); }
        }

        public void ToCustomText() {
            var f1 = new FieldText("Format", "Format to apply", "yyyy/MM/dd HH:mm:ss", ".+", "Format is empty");
            new InputBox(_excelapp, "To custom date-time", f1).Show(delegate {
                var format = f1.Value;
                new CellsOperator(_range).ForEachValue("@", delegate(ref object value) {
                    System.DateTime datetime;
                    if (Parser.ToDateTime(value, _culture, out datetime))
                        value = datetime.ToString(format, _culture);
                });
            });
        }

        public void ToDateText() {
            try {
                new CellsOperator(_range).ForEachValue("@", delegate(ref object value) {
                    System.DateTime datetime;
                    if (Parser.ToDateTime(value, _culture, out datetime))
                        value = datetime.ToString("yyyy/MM/dd");
                });
            } catch (Exception ex) { new FrmException(_excelapp, ex).ShowDialog(); }
        }

        public void ToTimeText() {
            try {
                new CellsOperator(_range).ForEachValue("@", delegate(ref object value) {
                    System.DateTime datetime;
                    if (Parser.ToDateTime(value, _culture, out datetime))
                        value = datetime.ToString("HH:mm:ss");
                });
            } catch (Exception ex) { new FrmException(_excelapp, ex).ShowDialog(); }
        }

        public void ToTotalMinutes() {
            try {
                new CellsOperator(_range).ForEachValue("General \"min\"", delegate(ref object value) {
                    System.DateTime datetime;
                    if (Parser.ToDateTime(value, _culture, out datetime))
                        value = datetime > _minDateTime ? (datetime - new DateTime(datetime.Year, 1, 1)).TotalMinutes : datetime.TimeOfDay.TotalMinutes;
                });
            } catch (Exception ex) { new FrmException(_excelapp, ex).ShowDialog(); }
        }

        public void ToTotalHours() {
            try {
                new CellsOperator(_range).ForEachValue("General \"hr\"", delegate(ref object value) {
                    System.DateTime datetime;
                    if (Parser.ToDateTime(value, _culture, out datetime))
                        value = datetime > _minDateTime ? (datetime - new DateTime(datetime.Year, 1, 1)).TotalHours : datetime.TimeOfDay.TotalHours;
                });
            } catch (Exception ex) { new FrmException(_excelapp, ex).ShowDialog(); }
        }

        public void ToTotalDays() {
            try {
                new CellsOperator(_range).ForEachValue("General \"day\"", delegate(ref object value) {
                    System.DateTime datetime;
                    if (Parser.ToDateTime(value, _culture, out datetime))
                        value = datetime > _minDateTime ? (datetime - new DateTime(datetime.Year, 1, 1)).TotalDays : datetime.TimeOfDay.TotalDays;
                });
            } catch (Exception ex) { new FrmException(_excelapp, ex).ShowDialog(); }
        }

        public void AddValue() {
            var f1 = new FieldNumber("Years", "Number of years to add", 0d);
            var f2 = new FieldNumber("Quaters", "Number of quaters to add", 0d);
            var f3 = new FieldNumber("Months", "Number of months to add", 0d);
            var f4 = new FieldNumber("Days", "Number of days to add", 0d);
            var f5 = new FieldNumber("Hours", "Number of hours to add", 0d);
            var f6 = new FieldNumber("Minutes", "Number of minutes to add", 0d);
            var f7 = new FieldNumber("Seconds", "Number of seconds to add", 0d);
            new InputBox(_excelapp, "Add value", f1, f2, f3, f4, f5, f6, f7).Show(delegate {
                var years = (int)f1.Value;
                var months = (int)f2.Value * 3 + (int)f3.Value;
                var ts = new TimeSpan((int)f4.Value, (int)f5.Value, (int)f6.Value, (int)f7.Value);
                new CellsOperator(_range).ForEachValue(null, delegate(ref object value) {
                    System.DateTime datetime;
                    if (Parser.ToDateTime(value, _culture, out datetime))
                        value = datetime.AddYears(years).AddMonths(months) + ts;
                });
            });
        }

        public void ToZone() {
            var zones = TimeInfo.GetItems();
            var f0 = new FieldList<TimeInfo.Item>("From zone GMT", null, zones, -1, true);
            var f1 = new FieldList<TimeInfo.Item>("To zone GMT", null, zones, -1, true);
            new InputBox(_excelapp, "To zone", f0, f1).Show(delegate {
                var ItemFrom = f0.Value;
                var ItemTo = f1.Value;
                var dateNow = System.DateTime.Now;
                var dtMin = new System.DateTime(1900, 1, 1);
                new CellsOperator(_range).ForEachValue(null, delegate(ref object value) {
                    DateTime dt;
                    if (Parser.ToDateTime(value, _culture, out dt)) {
                        if (dt < dtMin) {
                            var newDt = ItemTo.ToLocal(ItemFrom.ToUTC(new DateTime(dateNow.Year, dateNow.Month, dateNow.Day, dt.Hour, dt.Minute, dt.Second)));
                            value = new System.DateTime(1899, 12, 30, newDt.Hour, newDt.Minute, newDt.Second);
                        } else
                            value = ItemTo.ToLocal(ItemFrom.ToUTC(dt));
                    }
                });
            });

        }

    }
}
